import Vue from 'vue'
import Router from 'vue-router'
import menu from './menu'

Vue.use(Router)

export default new Router({
    mode: 'history', // Demo is living in GitHub.io, so required!
    linkActiveClass: 'is-active',
    scrollBehavior: () => ({
        y: 0
    }),
    routes: [
        {
            name: 'Home',
            path: '/',
            // component: require('../views/Home/index.vue')
            component: require('../views/index.vue')
        },
        ...generateRoutesFromMenu(menu), {
            path: '*',
            redirect: '/'
        }
    ]
})

// Menu should have 2 levels.
function generateRoutesFromMenu (menu = [], routes = []) {
    for (let i = 0, l = menu.length; i < l; i++) {
        let item = menu[i]
        if (item.path) {
            routes.push(item)
        }
        if (!item.component) {
            generateRoutesFromMenu(item.children, routes)
        }
    }
    return routes
}
